<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/11
 * Time: 17:04
 */
namespace app\home\model;

use app\home\model\Base;
use think\Model;
use think\Request;
use think\Db;
use think\Exception;
use think\Log;

class Order extends Base {
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取购卡订单列表，10条数据
     * 或某个订单的详情
     * @param array $where 搜索条件
     */
    public function getOrderListM($where = [])
    {
        try {
            $where['payer'] = $this->user_info['id'];
            $where['payee'] = $this->user_info['agent_id'];
            $where['pay_type'] = '11';
            $param = Request::instance()->param();
            if($param['id']){    // 订单id
                $where['id'] = $param['id'];
            }
            if($param['order_number']){   // 订单编号
                $where['order_number'] = $param['order_number'];
            }
            $order = Db::name('order')
                ->field('id,order_number,money,express,freight,remarks,order_info,status,addtime,paytime,dealtime,dealremarks,express_number')
                ->where($where)
                ->order('id desc')
                ->paginate(10,false);
            $data = $order->toArray();
            if(empty($data['data'])){   // 没有数据直接返回
                return false;
            }
            foreach($data['data'] as $key=>&$value){
                $value['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
                $value['paytime'] = date('Y-m-d H:i:s',$value['paytime']);
                $value['dealtime'] = date('Y-m-d H:i:s',$value['dealtime']);
                $value['status_text'] = $this->order_status[$value['status']];
                $value['order_info'] = json_decode($value['order_info'],true);
                $value['money'] = $value['money']/100;
                $value['freight'] = $value['freight']/100;
                $value['rowspan'] = count($value['order_info']['card']);   // 便于前台遍历
                $value['total_number'] += $value['order_info']['card'][$key]['number'];   // 总卡量
                $value['total_money'] = $value['money'] - $value['freight'];   // 合计金额，不包括运费
                // 是否划拨
                foreach($value['order_info']['card'] as &$ca){
                    $ca['flag'] = ($ca['flag'] == true) ? '是' : '否' ;
                }
            }
            return $data;
        } catch (Exception $e) {
            Log::write('获取用户订单列表异常：'.$e->getMessage(),'error');
        }
    }

    /**
     * 修改订单状态
     * @param int $status 订单状态 1：已支付 2：未支付 3：已取消 4：已发货
     * @return array 操作结果
     */
    public function changeOrderM($status = 0)
    {
        try {
            $param = Request::instance()->post();
            // where条件
            $where = " `status` = 2 AND `pay_type` = 11 AND `payer` = ".$this->user_info['id']." AND `payee` = ".$this->user_info['agent_id'];
            if($param['id']){   // 订单id
                $where .= " AND `id` = ".$param['id'];
            }
            if($param['order_number']){   // 订单编号
                $where .= " AND `order_number` = '".$param['order_number']."'";
            }
            $sql = "UPDATE yc_order SET `status` = ".$status." WHERE ".$where;
            $result = Db::execute($sql);
            if(!$result){
                return ['code'=>2,'msg'=>'操作失败！'];
            }
            return ['code'=>0,'msg'=>'操作成功！'];
        } catch (Exception $e) {
            Log::write('修改订单状态异常：'.$e->getMessage(),'error');
        }
    }


}